import Vue from 'vue';
import Router from 'vue-router';
import {
  getLocalStorage,removeLocalStorage
} from '@/utils/local-storage';

import home from './home';
import login from './login';

import {
  Toast
} from "vant"
Vue.use(Router);

const RouterModel = new Router({
  routes: [...home, ...login]
});

RouterModel.beforeEach((to, from, next) => {
  const {
    Authorization,
    user_id
  } = getLocalStorage(
    'Authorization',
    'user_id'
  );
  Toast.resetDefaultOptions()
  if (to.name !== 'addressManage' && to.name !== 'addressEdit') {
    removeLocalStorage('gotoSubmit')
  }
  if (to.name !== 'addressManage' && to.name !== 'addressEdit' && to.name !== 'orderFormSubmit') {
    removeLocalStorage('orderFormList')
  }
  if (to.name !== 'addressEdit') {
    removeLocalStorage('addressEditData')
  }
  if (to.name !== 'orderFormSubmit') {
    removeLocalStorage('selectedAddress')
  }
  if (!Authorization && !user_id) {
    if (to.meta.login) {
      next({
        name: 'login',
        query: {
          redirect: to.name
        }
      });
      return;
    }
  }
  next();
});

export default RouterModel;